package randomSelect;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class SelectService {
    public static void main(String[] args) {
        try {
            //姓名列表
            ArrayList<String> nameList = new ArrayList<>();
            //获取list.txt的绝对路径
            String path = SelectService.class.getResource("list.txt").getPath();
            System.out.println(path);
            //创建字符缓冲输入流
            BufferedReader br = new BufferedReader(new FileReader(path));
            //通过br逐行读取list.txt，将读取到的数据添加到nameList中
            String line = null;//此变量用于临时接收每一行读取到的数据
            while ((line = br.readLine()) != null) {
                nameList.add(line);//将读取到的数据添加到nameList中
            }
            Random random = new Random();
            //生成随机数，范围是0到nameList的长度-1
            int luckyNumber = random.nextInt(nameList.size());
            //输出抽到的幸运儿
            System.out.println(nameList.get(luckyNumber));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
